package com.zbl.zblproject.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.core.entity.News;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.mapper.NewsMapper;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.core.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2019/3/1
 */
@RestController
@RequestMapping("/facade")
@Api(tags = "门店管理")
public class FacadeController {

    @Autowired
    private NewsMapper newsMapper;

    @PostMapping("/add")
    @ApiOperation("添加新闻")
    public Result addNews(@ApiParam("标题") @RequestParam("title") String title,
                          @ApiParam("内容") @RequestParam("body") String body){
        if(StringUtil.isNullOrEmpty(title)){
            return ResultUtil.error("标题内容不能为空!");
        }
        if(StringUtil.isNullOrEmpty(body)){
            return ResultUtil.error("新闻内容不能为空!");
        }
        News news = new News();
        news.setTime(LocalDateTime.now());
        news.setTitle(title);
        news.setBody(body);
        if(newsMapper.insert(news) == 1){
            return ResultUtil.success("新闻添加成功!");
        }
        return ResultUtil.error("新闻内容添加失败!");
    }

    @GetMapping("/page/{current}/{size}")
    @ApiOperation("分页新闻")
    public Result list(@ApiParam("分页") @PathVariable int current,
                       @ApiParam("大小") @PathVariable int size){
        IPage<News> page = new Page<>(current, size);
        page = newsMapper.selectPage(page, null);
        return ResultUtil.success("查询成功!", page);
    }

}
